<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace plugins\matrix\controller; //Demo插件英文名，改成你的插件英文就行了
use cmf\controller\PluginMatrixRestBaseController;
use plugins\Demo\Model\PluginDemoModel;
use think\Db;
use think\Validate;

/**
 * 微信矩阵 统一登陆
 * Class ApiLoginController
 * @package plugins\matrix\controller
 */
class ApiLoginController extends PluginMatrixRestBaseController
{

    /**
     * 统一登陆入口
     * http://192.168.2.219/plugin/matrix/api_login/login/user_name/admin/password/123456.html
     */
    public function login()
    {
        $validate = new Validate([
            'user_name'          => 'require',
            'password'          => 'require'
        ]);

        $validate->message([
            'user_name.require'          => '账号不能为空!',
            'password.require'          => '密码不能为空!'
        ]);

        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }

        $where['user_name'] = trim($data['user_name']);
        $user =  Db::name("matrix_user")->where($where)->find();
        if (empty($user)) {
            $this->error("用户不存在!");
        } else {
            switch ($user['user_status']) {
                case 0:
                    $this->error('您已被拉黑!');
                case 2:
                    $this->error('账户还没有验证成功!');
            }
            if (!cmf_compare_password($data['password'], $user['user_pass'])) {
                $this->error("密码不正确!");
            }
            $token = cmf_generate_user_token($user["id"], 'matrix_admin');
            unset($user["create_time"], $user["user_status"], $user["user_pass"]);
            $this->success("OK!",['token' => $token,'user' => $user]);
        }
    }

    /**
     * 注册登陆账号
     * http://www.cmf.cn/plugin/matrix/api_login/register/name/zhangsan/user_name/admin.html
     */
    public function register()
    {
        $validate = new Validate([
            'name'          => 'require',
            'user_name'          => 'require'
        ]);
        $validate->message([
            'name.require'          => '姓名不能为空!',
            'user_name.require'          => '账号不能为空!'
        ]);

        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }

        $where['user_name'] = trim($data['user_name']);
        $user =  Db::name("matrix_user")->where($where)->find();
        if ($user) {
            $this->error('该账号已注册!');
        }else{
            $user['name'] = trim($data['name']);
            $user['user_name'] = trim($data['user_name']);
            $user['create_time'] = time();
            $user['user_status'] = 1;  //用户状态;0:禁用,1:正常,2:未验证
            $user['user_type']   = 1;   //管理员
            $user['user_pass']   = cmf_password('123456');  //默认密码:123456

            $result = Db::name("matrix_user")->insert($user);
            if (empty($result)) {
                $this->error("注册失败!");
            }
            $this->success('注册成功!');
        }
    }
}
